package com.bignerdranch.android.ecar.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.android.volley.Response;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class SignActivity extends BaseActivity implements View.OnClickListener {

    private boolean flag = false;
    private ImageButton sign_back;
    private EditText code;
    private Button code_but;
    private EditText pass;
    private Button register;
    private EditText phone;
    private int recLen = 60;
    private EventHandler eh;
    private String url;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

            if(recLen == 0) {
                code_but.setEnabled(true);
                code_but.setText(R.string.get_code);
                handler.removeCallbacks(runnable);
                code_but.setBackgroundColor(getResources().getColor(R.color.blue));
                recLen=60;
            }else{
                code_but.setEnabled(false);
                code_but.setText( --recLen  +"s");
                handler.postDelayed(this, 1000);
                code_but.setBackgroundColor(getResources().getColor(R.color.text_ddd_gray));
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        initView();
        eh = new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
//                        Toast.makeText(SignActivity.this,"提交验证码成功",Toast.LENGTH_SHORT).show();
                        Log.e("======","提交验证码成功");
                        flag = true;
                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功
//                        Toast.makeText(SignActivity.this,"获取验证码成功",Toast.LENGTH_SHORT).show();
//                        runOnUiThread();

                        Log.e("======","获取验证码成功");
                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        //返回支持发送验证码的国家列表
//                        Toast.makeText(SignActivity.this,"支持发送验证码的国家列表",Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ((Throwable) data).printStackTrace();
//                    Toast.makeText(SignActivity.this,"失败",Toast.LENGTH_SHORT).show();
                }
            }
        };
        SMSSDK.registerEventHandler(eh);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
//    一定要取消注册，否则容易出现内存泄漏
        SMSSDK.unregisterEventHandler(eh);
    }



    private void initView() {
        sign_back = (ImageButton) findViewById(R.id.sign_back);
        code = (EditText) findViewById(R.id.code);
        code_but = (Button) findViewById(R.id.code_but);
        pass = (EditText) findViewById(R.id.pass);
        register = (Button) findViewById(R.id.register);

        sign_back.setOnClickListener(this);
        code_but.setOnClickListener(this);
        register.setOnClickListener(this);
        phone = (EditText) findViewById(R.id.phone);
        phone.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_back:
                finish();
                break;
            case R.id.code_but:
                if (phone.getText().toString().length()==11){
                    Log.e("======","code_but");
                    handler.postDelayed(runnable, 1000);
                    SMSSDK.getVerificationCode("86",phone.getText().toString().trim() );
                }else {
                    Toast.makeText(SignActivity.this,"请正确输入手机号码",Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.register:
                SMSSDK.submitVerificationCode("86",phone.getText().toString().trim(),code.getText().toString().trim());
                if (flag){
                    Toast.makeText(SignActivity.this,"验证成功",Toast.LENGTH_SHORT).show();
                    signIn();
                    finish();
                }

                break;
        }
    }

    private void signIn(){
        url = URLBank.teacher_url+URLBank.register_url+"?phone="+phone.getText()+"&password="+pass.getText();
//        Log.e("------",url);
        RequestUtil.executeJSONObject(this, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    Log.e("------",o.getString("code"));
                    Log.e("------",o.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }


}
